//
//  SimPinManagerVC.swift
//  MIFI
//
//  Created by 金嗓子卡卡 on 2019/5/6.
//  Copyright © 2019 NJMifi. All rights reserved.
//

import UIKit
import SVProgressHUD

class SimPinManagerVC: RootVC {

    private let datas = [("PIN Switch",""),
                         ("Confirm PIN","")]
    
    private lazy var nameLabel: UILabel = {
        let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 50))
        label.right = screenWidth - 30
        label.backgroundColor = clearColor
        label.text = "Enter PIN"
        label.textAlignment = .right
        return label
    }()
    
    private lazy var pinSwitch: UISwitch = {
        let sw = UISwitch(frame: CGRect(x: 0, y: 10, width: 50, height: 30))
        sw.right = screenWidth - 8
        if UserInfoManager.shared().pin_status == "1" {
           sw.isOn = true
        }
        sw.addTarget(self, action: #selector(switchChangeValue(sender:)), for: .valueChanged)
        sw.tag = 0
        
        return sw
    }()
    
    private lazy var tableView: UITableView = {
        let tab = UITableView(frame: CGRect(x: 0, y: naviView!.bottom, width: screenWidth, height: screenHeight - naviView!.bottom), style: .grouped)
        tab.backgroundColor = whiteColor
        tab.rowHeight = 50
        tab.delegate = self
        tab.dataSource = self
        tab.separatorStyle = .none
        tab.isScrollEnabled = false
        return tab
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func configView() {
        configNaviView(title: "SIM/PIN Manager")
        view.addSubview(tableView)
        
        rightBtn?.backgroundColor = clearColor
        rightBtn?.setTitle("OK", for: .normal)
        rightBtn?.setTitleColor(whiteColor, for: .normal)
        rightBtn?.titleLabel?.font = UIFont.systemFont(ofSize: 18)
        
    }
    
    private func showInput() {
        var input = ""
        if nameLabel.text != nil && nameLabel.text != "Enter PIN" {
            input = nameLabel.text!
        }
        let alertView = InputAlertView(input: input)
        alertView.handle = ({[weak self] content in
            self?.nameLabel.text = content
        })
        let alertVC = CustomAlertVC(contentType: .animation(alertView))
        alertVC.show()
    }
    
    override func rightBtnClicked(sender: UIButton) {
        guard !isEmpty(string: nameLabel.text) && nameLabel.text != "Enter PIN" else {
            SVProgressHUD.showError(withStatus: "The field cannot be empty.")
            return
        }
        
        
        
        if UserInfoManager.shared().pin_status == "1" && pinSwitch.isOn {
            
            SVProgressHUD.showError(withStatus: "pin had opened")
            return
        }
        
        if UserInfoManager.shared().pin_status == "0" && !pinSwitch.isOn {
            
            SVProgressHUD.showError(withStatus: "pin had closed")
            return
        }
        
        net_setPinSwitch(status: pinSwitch.isOn, code: nameLabel.text!) { [weak self] (success) in
            
        }
    }
    
    @objc private func switchChangeValue(sender:UISwitch) {
        
    }
}

extension SimPinManagerVC : UITableViewDelegate, UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datas.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
        cell.textLabel?.text = datas[indexPath.row].0
        cell.textLabel?.font = UIFont.systemFont(ofSize: 18)
        cell.textLabel?.textColor = textGray
        cell.selectionStyle = .none
        
        
        nameLabel.font = cell.textLabel?.font
        nameLabel.textColor = cell.textLabel?.textColor
        
        if indexPath.row == 0 {
            cell.contentView.addSubview(pinSwitch)
        }else {
            cell.accessoryType = .disclosureIndicator
            cell.contentView.addSubview(nameLabel)
        }
        
        let line = UIView(frame: CGRect(x: 8, y: 50 - 0.5, width: screenWidth - 16, height: 0.5))
        line.backgroundColor = textGray
        cell.contentView.addSubview(line)
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row == 1 && pinSwitch.isOn{
            showInput()
        }
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return nil
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return nil
    }
}
